#include "app_msg.h"
#include "data.h"
#include "usart.h"
#include "timer.h"
#include "log.h"

void UART1_Send_Msg(u8 cmd ,u8 *data, u16 dataLen)
{
  u8 tmp[MAX_UART_DATA_LEN] = {0};
  MSG_HEADER* msg = (MSG_HEADER*)tmp;

  memset(tmp, 0, MAX_UART_DATA_LEN);
  if (msg)
  {
    u16 i = 0;
    msg->sign1 = 0xAA;
    msg->cmd = cmd;

    if(dataLen == 0 || data == NULL)
    {
      msg->len = 0;

      msg->data[dataLen] = msg->cmd + (u8)dataLen + (u8)(dataLen >> 8);
      
      uart1_transData(tmp, sizeof(MSG_HEADER) + dataLen + 3);
      return;
    }

    msg->len = dataLen;
    memcpy(msg->data, data, dataLen);

    msg->data[dataLen] = msg->cmd + (u8)dataLen + (u8)(dataLen >> 8);            

    for(i = 0; i < dataLen; i++)
    {
      msg->data[dataLen] += data[i];
    }
  }
  LOG_DEBUG("send uaer1 msg:%d",sizeof(MSG_HEADER) + dataLen + 1);
	LOG_BIN(tmp,sizeof(MSG_HEADER) + dataLen + 1);
  uart1_transData(tmp, sizeof(MSG_HEADER) + dataLen + 1);
}

void WIFI_Send_AllInfoHandler(void)
{
  STM_DATA *pData = data_getData();
  u8 tmp[MAX_UART_DATA_LEN] = {0};
  ALLINFO_MSG *msg = (ALLINFO_MSG *)tmp;
  u8 max_angle;
  msg->heart = pData->heart;
  msg->spo = pData->spo2;
  msg->temperature = pData->temperature;
  msg->pressure = pData->pressure;
  max_angle = pData->pitch;
  if(max_angle < (pData->roll))
  {
    max_angle = pData->roll;
  }
  if(max_angle < (pData->yaw))
  {
    max_angle = pData->yaw;
  }
  msg->angle = max_angle;
  msg->Longitude = pData->Gps_data.longitude/100000.0;
  msg->Latitude = pData->Gps_data.latitude/100000.0;
  
  UART1_Send_Msg(CMD_SEND_ALLINFO, tmp, ALLINFO_MSG_LEN);
}

void WIFI_Send_SettingHandler(void)
{
  STM_DATA *pData = data_getData();
  u8 tmp[MAX_UART_DATA_LEN] = {0};
  SET_MSG *msg = (SET_MSG *)tmp;
  
  memset(msg, 0, 4);
  memcpy(msg, pData->Alarm_Line, 4);
  
  UART1_Send_Msg(CMD_SEND_SETTING, tmp, SET_MSG_LEN);
}

void WIFI_Send_AlarmInfoHandler(void)
{
  STM_DATA *pData = data_getData();
  u8 tmp[MAX_UART_DATA_LEN] = {0};
  ALARM_MSG *msg = (ALARM_MSG *)tmp;
  
  msg->alarm_angle = pData->MPU_ALARM_FLAG;
  msg->alarm_heart = pData->HEART_ALARM_FLAG;
  msg->alarm_tempe = pData->TEMP_ALARM_FLAG;
  msg->alarm_press = pData->PRES_ALARM_FLAG;

  UART1_Send_Msg(CMD_SEND_ALARM, tmp, ALARM_MSG_LEN);
}

typedef void (* WIFI_LOOP_HANDLER_PROC)(void);

typedef struct
{
    WIFI_LOOP_HANDLER_PROC fun;
} WIFI_DEFAULT_HANDLER;

static WIFI_DEFAULT_HANDLER wifi_default_handler[] = 
{
    {WIFI_Send_AllInfoHandler},
    {WIFI_Send_SettingHandler},
//    {WIFI_Send_AlarmInfoHandler}
};



void WIFI_TIMER_HANDER(void)
{
    static u8 wifi_loop_pos = 0;

    u8 idx = wifi_loop_pos % (sizeof(wifi_default_handler) / sizeof(wifi_default_handler[0]));
    
    if(wifi_default_handler[idx].fun)
    {
        wifi_default_handler[idx].fun();
    }
    wifi_loop_pos ++;
}


static BOOL WIFI_isCheckSum(MSG_HEADER *msg)
{
    u8 i = 0;

    u8 checksum = msg->cmd + (u8)(msg->len) + (u8)((msg->len) >> 8);
    for(i = 0;i < msg->len;i++)
    {
        checksum += msg->data[i];
    }
    if(checksum == msg->data[msg->len])
    {
        return L_TRUE;
    }
    LOG_DEBUG("WIFI_isCheckSum ERR: 0x%X 0x%X",checksum,msg->data[msg->len]);
    return L_FALSE;
}


void WIFI_Recive_MsgHandler(u8 *data,u16 len)
{
  SET_REC_MSG *msg = (SET_REC_MSG *)data;
  if(msg->sign1 == 0xAA && (msg->cmd > 0) && (msg->cmd < 5))
  {
    if((msg->cmd + msg->data) == (u8)(msg->check))
    {
      data_getData()->Alarm_Line[msg->cmd - 1] = msg->data;
      LOG_DEBUG("SETTING [%d] %d",msg->cmd,msg->data);
    }
  }
}


//static void WIFI_Allinfo_Rsp(MSG_HEADER *msg)
//{
//  LOG_DEBUG("WIFI_Allinfo_Rsp");
//}

//static void WIFI_Setting_Rsp(MSG_HEADER *msg)
//{
//  LOG_DEBUG("WIFI_Setting_Rsp");
//}

//static void WIFI_Alarm_Rsp(MSG_HEADER *msg)
//{
//  LOG_DEBUG("WIFI_Alarm_Rsp");
//}

//typedef int (*MSG_PROC)(MSG_HEADER *);
//typedef struct
//{
//    u8 cmd;
//    MSG_PROC  action;
//} PROC_MAP;

//PROC_MAP bms_msg_procs[] = 
//{
//    {CMD_SEND_ALLINFO,         WIFI_Allinfo_Rsp},
//    {CMD_SEND_SETTING,         WIFI_Setting_Rsp},
//    {CMD_SEND_ALARM,           WIFI_Alarm_Rsp}
//};

//void WIFI_Recive_MsgHandler(u8 *data,u16 len)
//{
//  u8 i = 0;
//  MSG_HEADER *msg = (MSG_HEADER *)data;
//  if(WIFI_isCheckSum(msg))
//  {
//    LOG_DEBUG("cmd:%#x", msg->cmd);
//    for(i = 0; i < sizeof(bms_msg_procs)/sizeof(bms_msg_procs[0]); i++)
//    {
//        if(bms_msg_procs[i].cmd == msg->cmd)
//        {
//            if(bms_msg_procs[i].action)
//            {
//                bms_msg_procs[i].action(msg);
//            }
//        }
//    }
//  }
//}
